<?php
/**
 * 商品收藏 api
 * @author maniac
 */
namespace app\api\controller\v1;

use app\common\model\GoodsCollect AS GoodsCollectModel;
use app\facade\GoodsCollect AS GoodsCollectFacade;

class GoodsCollect extends Base
{
    /**
     * 收藏列表
     */
    public function collectList()
    {
        $userId = $this->checkUserToken();
        $page   = input('post.page/d', 1);
        $limit  = input('post.limit/d', 10);
        $offset = ($page - 1) * $limit;
        $limit  = $offset .','. $limit;
        try {
            $list = GoodsCollectModel::field('id,goods_id,collect_time')->append(['goods'])
                ->where('userid', $userId)->where('type',0)->order('id DESC')->limit($limit)->select();
            return j(0, '获取信息成功', $list);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
    
    /**
     * 删除收藏的商品
     */
    public function deleteCollect()
    {
        $userId = $this->checkUserToken();
        $id     = input('post.id', 0);
        if (!$id) {
            return j(1, '请选择要移除的商品');
        }
        try {
            GoodsCollectFacade::deleteCollect($id);
            return j(0, '移除成功');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /**

     * [取消]收藏商品

     */

    public function collect()

    {

        $userId  = $this->checkUserToken();

        $goodsId = input('post.goods_id/d', 0);

        if (!$goodsId) {

            exception('请选择要收藏的商品');

        }

        try {

            $result = GoodsCollectFacade::collect($userId, $goodsId);

            return j(0, $result);

        } catch (\Exception $e) {
            return j(1, $e->getMessage());

        }

    }

}
